<?php
namespace Home\Controller;
use Think\Controller;
use Think\Page;
class IndexController extends CommonController {

	public function index(){

		$this->display('Index/default');
	}

	public function menu(){

		$this->display('Index/index');
	}

	public function media(){

		$this->display('Index/media');
	}

	public function impression(){

		$this->display('Index/impression');
	}


	public function house(){

		$this->display('Index/house');
	}

	public function vest(){

		$this->display('Index/vest');
	}

	public function fillvest(){
		$m = D('Question');
		 
		if(!$m->create()){
			$this->error($m->getError());
		}
		$idNum=$m->add();

		$this->display('Index/message');
	}

	public function map(){

		$this->display('Index/map');
	}
	//for weixin
	// public function weixin(){
	// 	$echoStr = $_GET["echostr"];
	// 	if($this->checkSignature()){
	//    	echo $echoStr;
	//    	exit;
	//    }
	// }

	public function weixin(){
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

		// error_log($postStr."\r\n", 3, "weixin.log");
		if (!empty($postStr)){

			$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
			$fromUsername = $postObj->FromUserName;
			$toUsername = $postObj->ToUserName;
			$key = trim($postObj->EventKey);
			$event = strtolower(trim($postObj->Event));
			// error_log($event."\r\n", 3, "weixin.log");
			$time = time();

			$textTpl = "<xml>
			<ToUserName><![CDATA[%s]]></ToUserName>
			<FromUserName><![CDATA[%s]]></FromUserName>
			<CreateTime>%s</CreateTime>
			<MsgType><![CDATA[%s]]></MsgType>
			<Content><![CDATA[%s]]></Content>
			<FuncFlag>0</FuncFlag>
			</xml>"; 

			if($key == 'zj-phone' && $event == 'click'){

				$contentStr = "销售热线：0573-82956666，点击直接拨打！";
				 
				 
			}else if($event == 'subscribe'){
				$contentStr = "你好！欢迎关注紫金·艺境！";
			}else{
				$contentStr = 'server error...';
			}
			$msgType = "text";
			$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
			echo $resultStr;
		}else {
			echo "no post...";
			exit;
		}
	}

	private function checkSignature(){
		$signature = $_GET["signature"];
		$timestamp = $_GET["timestamp"];
		$nonce = $_GET["nonce"];

		$token = 'ziweixinjin';
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );

		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}

}